<template>
  <div class="equipdocs-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>
        <el-button :icon="DocumentAdd" plain type="primary" @click="openAdd">
          新增
        </el-button>
        <el-button
          :disabled="!currentRef"
          :icon="Edit"
          plain
          type="warning"
          @click="update"
        >
          修改
        </el-button>
        <el-button
          :disabled="!currentRef"
          :icon="Delete"
          plain
          type="danger"
          @click="deleteEquip"
        >
          删除
        </el-button>
        <el-button :icon="Printer" plain type="primary" @click="toPrint">
          打印
        </el-button>
        <!-- <el-button :icon="Edit" plain type="warning" @click="toUpdate()">
          停用
        </el-button>
        <el-button :icon="Edit" plain type="primary" @click="toUpdate()">
          启用
        </el-button> -->
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :cell-style="{ padding: '6px 0px' }"
      :data="equipDocs"
      highlight-current-row
      @current-change="handleCurrentChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" width="45" />
      <el-table-column label="设备编码" prop="equipCode" width="180" />
      <el-table-column label="设备名称" prop="equipName" width="180" />
      <el-table-column label="设备型号" prop="equipSpec" width="180" />
      <el-table-column label="工作中心" prop="wcName" width="120" />
      <el-table-column label="状态" prop="stateName" width="120" />
      <el-table-column label="生产设备" prop="eqTypeName" width="120" />
      <el-table-column label="可上机" prop="stateName" width="120">
        <template #default="scope">
          <el-icon v-if="scope.row.isEnable" color="#30DF80" size="20">
            <Select />
          </el-icon>
          <el-icon v-else color="#DF3730" size="20">
            <SemiSelect />
          </el-icon>
        </template>
      </el-table-column>
      <el-table-column label="供应商" prop="vender" width="220" />
      <el-table-column label="创建人" prop="creator" width="120" />
      <el-table-column label="创建时间" prop="createTime" width="200" />
      <el-table-column label="修改人" prop="updateUser" width="120" />
      <el-table-column label="修改时间" prop="updateTime" width="200" />
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <queryDialog ref="queryDialogRef" @success-callback="queryCallback" />
    <addDialog ref="addDialogRef" @success-callback="addCallback" />
    <updateDialog ref="updateDialogRef" @success-callback="updateCallback" />
    <deleteEquipdoc
      ref="deleteEquipdocRef"
      @success-callback="deleteCallback"
    />
  </div>
</template>

<script setup>
  import {
    Search,
    DocumentAdd,
    Select,
    SemiSelect,
    Edit,
    Delete,
    Printer,
  } from '@element-plus/icons-vue'
  import { getEqlistsPageApi } from '@/api/equip/equip_api'
  import { localStorageConfig } from '@/config/localstorage.config'
  import queryDialog from './modules/queryDialog.vue'
  import addDialog from './modules/addDialog.vue'
  import updateDialog from './modules/updateDialog.vue'
  import deleteEquipdoc from './modules/deleteEquipdoc.vue'
  import hiPrint from '@/utils/hiPrint.js'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const queryDialogRef = ref()
  const equipQueryRef = ref({ pkCorp: pkCorp, state: 2000 })
  const equipDocs = ref()
  const queryForm = ref()
  const addDialogRef = ref()
  const updateDialogRef = ref()
  const currentRef = ref()
  const deleteEquipdocRef = ref()
  const selectedRows = ref()

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 20,
    total: 0,
  })

  const openQueryDialog = () => queryDialogRef.value.openDialog()

  const queryCallback = (form) => {
    queryForm.value = form
    getEquiplist()
  }

  const getEquiplist = async () => {
    const load = $baseLoading()
    try {
      let params = {
        ...equipQueryRef.value,
        ...queryForm.value,
        ...paginationParams,
      }
      const { code, msg, data } = await getEqlistsPageApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        equipDocs.value = data.list
        paginationParams.total = data.total
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const handleCurrentChange = (rowData) => (currentRef.value = rowData)

  const openAdd = () => addDialogRef.value.openDialog()

  const addCallback = () => getEquiplist()

  const update = () => updateDialogRef.value.openDialog(currentRef.value)

  const updateCallback = () => getEquiplist()

  const deleteEquip = () => deleteEquipdocRef.value.openDialog(currentRef.value)

  const deleteCallback = () => getEquiplist()

  const handleSelectionChange = (rows) => {
    selectedRows.value = rows
  }

  const toPrint = () => {
    hiPrint('equip_001', selectedRows.value)
  }

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
    getEquiplist()
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
    getEquiplist()
  }
  //#endregion

  onMounted(() => getEquiplist())
</script>
